package pp.strategy.worktime;

import pp.IWorkTimeStrategy;
import properties.AutoBGProperties;
import properties.WorkTimeBean;
import properties.WorkTimes;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class HandWorkTimeStrategy implements IWorkTimeStrategy {
    private static final List<String> weeks = Arrays.asList("one", "two", "three", "four", "five", "six", "seven");

    @Override
    public Map<String, WorkTimeBean> workTime(AutoBGProperties autoBGProperties) {
        Map<String, WorkTimeBean> workTimeBeans = new HashMap<>(7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(autoBGProperties.getFirstDay());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < 7; i++) {
            String week = weeks.get(i);
            WorkTimeBean weekBean = buildAutoBGbean(autoBGProperties.getWorkTimes(), week);
            if (weekBean.getOvertimeHours().equalsIgnoreCase("0") &&
                    weekBean.getStandardHours().equalsIgnoreCase("0")) {
                continue;
            }
            workTimeBeans.put(sdf.format(new Date(parse.getTime() + (3600L * 24 * 1000) * i)), weekBean);
        }
        return workTimeBeans;
    }


    public WorkTimeBean buildAutoBGbean(WorkTimes workTimes, String week) {
        try {
            Field field = workTimes.getClass().getDeclaredField(week);
            field.setAccessible(true);
            Object o = field.get(workTimes);
            return (WorkTimeBean) o;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
